<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\ding;

use app\common\model\ding\Food as FoodModel;
use think\facade\Db;

/**
 * 菜品模型
 * Class Article
 * @package app\api\model
 */
class Food extends FoodModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'store_id',
        'create_time',
        'update_time',
    ];

    public static function increased(int $shopId, int $categoryId, array $contentList)
    {
        static::deleteAll(['shop_id' => $shopId]);

        $dataset = [];
        foreach ($contentList as $content) {
            $dataset[] = [
                'category_id' => $categoryId,
                'shop_id'     => $shopId,
                'name'        => $content['name'],
                'cost_price'  => $content['cost_price'],
                'price'       => $content['price'],
                'store_id'    => self::$storeId,
            ];
        }
        return (new static )->addAll($dataset);
    }

    public function add(int $shopId, $data)
    {

        return $this->transaction(function () use ($shopId, $data) {

            $data['shop_id']     = $shopId;
            $data['sort']        = Db::name('ding_food_category')->where('category_id', $data['category_id'])->value('category_id');
            $data['create_time'] = time();
            $data['store_id']    = self::$storeId;
            $this->save($data);

            return true;
        });
    }
    public function edit($data)
    {

        $this->save($data);

        return true;
    }
    public function del()
    {

        $this->delete();

        return true;
    }
    /**
     * 获取文章列表
     * @param int $categoryId
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(int $shopId = 0, int $limit = 8)
    {
        // 检索查询条件
        $filter                  = [];
        $shopId > 0 && $filter[] = ['shop_id', '=', $shopId];
        // 获取列表数据
        return $this->with(['image'])

            ->where($filter)

            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc'])
            ->paginate($limit);
    }

}
